import subprocess

ALIYUN_REPO = "192.168.3.12:5008"

IMAGES = [
    "docker.io/rancher/mirrored-pause:3.6",
    "docker.io/rancher/mirrored-coredns-coredns:1.12.1",
    "docker.io/rancher/klipper-helm:v0.9.8-build20250709",
    "docker.io/rancher/local-path-provisioner:v0.0.31",
    "docker.io/rancher/mirrored-metrics-server:v0.7.2"
]

def run_command(cmd):
    print(f"运行命令: {cmd}")
    result = subprocess.run(
        cmd, shell=True, check=True,
        stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True,
        encoding='utf-8', errors='ignore'  # 避免因编码报错
    )
    print(result.stdout)
    if result.stderr:
        print(result.stderr)

# 1️⃣ 先全部拉取
# for image in IMAGES:
#     print(f"拉取镜像: {image}")
#     run_command(f"docker pull {image}")

# 2️⃣ 再全部推送
for image in IMAGES:
    target_image = image.replace('docker.io', ALIYUN_REPO)
    print(f"处理并推送镜像: {image} -> {target_image}")
    run_command(f"docker tag {image} {target_image}")
    run_command(f"docker push {target_image}")
    print(f"完成镜像推送: {target_image}\n")
